//
//  ModelBankCard.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/11.
//
//

#import "ModelBankCard.h"
#import "ProductNameAndSloganList.h"

NSString *const kModelBankCardStep = KEY_step;
NSString *const kModelBankCardData = RESPONSE_DATA;
NSString *const kModelBankCardProductNameAndSloganList = KEY_productNameAndSloganList;

@interface ModelBankCard ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelBankCard

@synthesize step = _step;
@synthesize data = _data;
@synthesize productNameAndSloganList = _productNameAndSloganList;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.step = [self objectOrNilForKey:kModelBankCardStep fromDictionary:dict];
        self.data = [self objectOrNilForKey:kModelBankCardData fromDictionary:dict];
        NSObject *receivedProductNameAndSloganList = [dict objectForKey:kModelBankCardProductNameAndSloganList];
        NSMutableArray *parsedProductNameAndSloganList = [NSMutableArray array];
    
        if ([receivedProductNameAndSloganList isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedProductNameAndSloganList) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedProductNameAndSloganList addObject:[ProductNameAndSloganList modelObjectWithDictionary:item]];
                }
            }
        } else if ([receivedProductNameAndSloganList isKindOfClass:[NSDictionary class]]) {
            [parsedProductNameAndSloganList addObject:[ProductNameAndSloganList modelObjectWithDictionary:(NSDictionary *)receivedProductNameAndSloganList]];
        }
        self.productNameAndSloganList = [NSArray arrayWithArray:parsedProductNameAndSloganList];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.step forKey:kModelBankCardStep];
    [mutableDict setValue:self.data forKey:kModelBankCardData];
    NSMutableArray *tempArrayForProductNameAndSloganList = [NSMutableArray array];
    for (NSObject *subArrayObject in self.productNameAndSloganList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            [tempArrayForProductNameAndSloganList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            [tempArrayForProductNameAndSloganList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForProductNameAndSloganList] forKey:kModelBankCardProductNameAndSloganList];
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
